
package com.fl.util.compress;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

/**
 * BZip2文件格式压缩实现<br>
 * 调用Aapache Commons Compress包中的
 * 
 * <pre>
 * BZip2CompressorInputStream 
 * BZip2CompressorOutputStream
 * </pre>
 * 
 * 实现<br>
 * 
 */
public class BZip2Compressor extends AbstractFileCompressor {

    /**
     * 构造函数 <br>
     * */
    public BZip2Compressor() {
        super();
    }

    /**
     * 使用Bzip2算法压缩文件<br>
     * 
     * @see com.unionpay.common.util.compress.FileCompressor#compress(java.io.File, java.io.File)
     */
    @Override
    public void compress(File srcFile, File destFile) throws IOException {
        OutputStream out = null;
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(srcFile), buffSize);
            out = new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), buffSize));
            copyStreams(is, out);
        } finally {
            closeStreams(is);
            closeStreams(out);
        }

    }

    /**
     * 使用Bzip2算法解压文件<br>
     * 注意:解压文件名取源文件名的.字符前面的部分<br>
     * 
     * @see com.unionpay.common.util.compress.FileCompressor#decompress(java.io.File, java.io.File)
     */
    @Override
    public void decompress(File srcFile, File destDir) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            File destFile = new File(destDir, getBaseName(srcFile.toString()));
            is = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), buffSize));
            os = new BufferedOutputStream(new FileOutputStream(destFile), buffSize);
            copyStreams(is, os);
        } finally {
            closeStreams(is);
            closeStreams(os);
        }
    }
}
